#ifndef __LOGGER_INCLUDE__
#define __LOGGER_INCLUDE__

#include <string>

#include "common.h"
#include "dk_lock.h"

class Logger {
public:
  enum { BUFF_SIZE = 2048 };

  Logger()
    : fp_(NULL),
      debug_level_(-1),
      rotate_size_(-1),
      current_size_(-1),
      flush_(true)
  {
    memset(&rotate_tm_, 0, sizeof(rotate_tm_));
  }

  ~Logger() {
    if (fp_ != NULL) {
      fclose(fp_);
    }
  }
  

  bool Init(const char *filename,
            int debug_level,
            bool flush,
            int rotate_size);

  void set_debug_level(int level) {
    debug_level_ = level;
  }

  int get_debug_level() {
    return debug_level_;
  }

  void Log(int level, const char *fmt, ...);

private:
  bool OpenFile(const char *filename);
  bool RotateLog();

private:
  FILE *fp_;
  int  debug_level_;
  int  rotate_size_;
  int  current_size_;
  bool flush_;
  struct tm rotate_tm_;
  std::string filename_;
  Mutex mtx_;
};

#define LOG_DEBUG(args...) g_logger->Log(4, args)
#define LOG_INFO(args...) g_logger->Log(3, args)
#define LOG_WARN(args...) g_logger->Log(2, args)
#define LOG_ERROR(args...) g_logger->Log(1, args)

#endif
